package cn.itcast.mq.listener;

import cn.itcast.mq.config.CommonConfig;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener {

    @RabbitListener(queues = CommonConfig.DIRECT_QUEUE)
    public void listenMessage(String msg) {
        System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
        System.out.println(1 / 0);//模拟出现异常
        System.out.println("消息处理成功");
    }

    @RabbitListener(queues = CommonConfig.ERROR_QUEUE)
    public void listenErrorMessage(String msg) {
        System.out.println("接收到失败的消息：【" + msg + "】");
    }

    /**
     * 监听死信队列消息
     *
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "dl.queue"),
            exchange = @Exchange(name = "dl.exchange"),
            key = "dl.key"))
    public void listenDlMessage(String msg) {
        System.out.println("收到死信消息：" + msg);
    }

    /**
     * 惰性队列监听消息
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(name = "x-queue-mode", value = "lazy")
    ))
    public void listenLazyMessage(String msg) {
        System.out.println("消费者接收到lazy.queue的消息：【" + msg + "】");
    }
}
